package com.ct.repository.commons;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.repository.convert.CopyConvertUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 飞哥
 * @date 2023-08-16$ 19:49$
 */
public interface ITransfterBaseService {

    /**
     * @param clz
     * @param <T>
     * @param <R>
     * @return
     */
    default <T, R> R tranferBo(T pojo, Class<R> clz) {
        try {
            R r = CopyConvertUtils.copyProperties(pojo, clz);
            return r;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * @param clz
     * @param <T>
     * @param <R>
     * @return
     */
    default <T, R> List<R> tranferListBo(List<T> list, Class<R> clz) {
        try {
            List<R> collect = list.stream()
                    .map(pojo -> this.tranferBo(pojo, clz))
                    .collect(Collectors.toList());
            return collect;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @param clz
     * @param <T>
     * @param <R>
     * @return
     */
    default <T, R> Page<R> tranferPageBo(Page<T> poPage, Class<R> clz) {
        List<R> hotelBos = tranferListBo(poPage.getRecords(), clz);
        Page<R> boPage = new Page<>();
        boPage.setTotal(poPage.getTotal());
        boPage.setPages(poPage.getPages());
        boPage.setSize(poPage.getSize());
        boPage.setCurrent(poPage.getCurrent());
        boPage.setRecords(hotelBos);
        return boPage;
    }

    /**
     * @param clz
     * @param <T>
     * @param <R>
     * @return
     */
    default <T, R> PageUtils<R> tranferPageUtilsBo(Page<T> poPage, Class<R> clz) {
        List<R> hotelBos = tranferListBo(poPage.getRecords(), clz);
        PageUtils<R> boPage = PageUtils.getEmptyInstance();
        boPage.setTotal(poPage.getTotal());
        boPage.setPages(poPage.getPages());
        boPage.setSize(poPage.getSize());
        boPage.setCurrent(poPage.getCurrent());
        boPage.setRecords(hotelBos);
        return boPage;
    }

    /**
     * @param clz
     * @param <T>
     * @param <R>
     * @return
     */
    default <T, R> PageUtils<R> tranferPageUtilsBo(PageUtils<T> poPage, Class<R> clz) {
        List<R> hotelBos = tranferListBo(poPage.getRecords(), clz);
        PageUtils<R> boPage = PageUtils.getEmptyInstance();
        boPage.setTotal(poPage.getTotal());
        boPage.setPages(poPage.getPages());
        boPage.setSize(poPage.getSize());
        boPage.setCurrent(poPage.getCurrent());
        boPage.setRecords(hotelBos);
        return boPage;
    }
}
